package com.cskaoyan.service;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.vo.AddressVO;
import com.cskaoyan.mapper.AddressMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public List<AddressVO> addressList() {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        List<Address> addressList = addressMapper.selectListByUserId(userId);
        ArrayList<AddressVO> addressVOList = new ArrayList<>();
        for (Address address : addressList) {
            List<Integer> list = new ArrayList<>();
            list.add(address.getAreaId());
            list.add(address.getCityId());
            list.add(address.getProvinceId());
            List<String> regionList = addressMapper.selectRegionListByIdList(list);
            String detailedAddress = null;
            if (regionList.size()==3){
                detailedAddress = "" + regionList.get(0)+regionList.get(1)+regionList.get(2) + address.getAddress();
            }
            AddressVO addressVO = new AddressVO(address.getIsDefault(),
                                                    detailedAddress,
                                                    address.getName(),
                                                    address.getMobile(),
                                                    address.getId());
            addressVOList.add(addressVO);
        }
        return addressVOList;
    }

    @Override
    public Map<String, Object> addressDetail(Integer id) {
        Map<String, Object> map = addressMapper.selectMapById(id);
        ArrayList<Integer> list = new ArrayList<>();
        list.add((Integer) map.get("areaId"));
        list.add((Integer) map.get("cityId"));
        list.add((Integer) map.get("provinceId"));
        List<String> regionList = addressMapper.selectRegionListByIdList(list);
        map.put("provinceName",regionList.get(0));
        map.put("cityName",regionList.get(1));
        map.put("areaName",regionList.get(2));
        return map;
    }

    @Override
    public int saveAddress(Address address) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getPrincipal();
        Date date = new Date();
        if (address.getId() == 0){
            address.setId(null);
            address.setAddTime(date);
            address.setUserId(userId);
            address.setDeleted(false);
            address.setUpdateTime(date);
            addressMapper.insertSelective(address);
        }else {
            address.setUpdateTime(date);
            addressMapper.updateByPrimaryKeySelective(address);
        }
        return address.getId();
    }

    @Override
    public int deleteAddress(Integer id) {
        Address address = new Address();
        address.setId(id);
        address.setUpdateTime(new Date());
        address.setDeleted(true);
        int update = addressMapper.updateByPrimaryKeySelective(address);
        return update;
    }
}
